#include "qelib.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#ifdef _WIN32
#include <conio.h>
#endif

QELOG_DOMAIN("ldtest");

static void signal_handler(int signal)
{
    qe_notice("exit");
    exit(EXIT_SUCCESS);
}

int shell_write(char *data, int len)
{
    int n = len;
    while (n--) {
        putchar(*data++);
    }
    return len;
}


static void key_exit(qe_shell *sh)
{
    qe_notice("exit");
    exit(EXIT_SUCCESS);
}
QE_SHELL_EXPORT_KEY(0x03000000, key_exit, exit);

int shell_read(char *data, int len)
{
    int n = len;
    while (n--)
    {
#ifdef _WIN32
        *data++ = _getch();
#else
        *data++ = getchar();
#endif
    }
    return len;
}

extern qe_shell_command qe_shellcommand_start;
extern qe_shell_command qe_shell_commandhelp;
extern qe_shell_command qe_shellcommand_end;

int main(int argc, char **argv)
{
    signal(SIGINT, signal_handler);
    qelog_init(QELOG_DEBUG, QELOG_LV|QELOG_DM|QELOG_CL);
    qelog_domain_set_level(QELOG_DOMAIN_SHELL, QELOG_DEBUG);
    qe_shell shell;
    qe_memset(&shell, 0x0, sizeof(shell));
    qe_shell_init(&shell, 512,
        QE_SHELL_DEFAULT_USER, shell_read, shell_write);
    qe_shell_task(&shell);
    return 0;
}